package com.aijk.ehm.sys.service;

import com.aijk.ehm.sys.model.SysAdminExample;
import com.aijk.ehm.sys.mapper.SysAdminMapper;
import com.aijk.ehm.sys.model.SysAdmin;
import com.aijk.ehm.sys.model.qo.SysAdminQo;
import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>  </p>
 * User: chendw
 * Date: 2016/11/15 17:14
 */
@Service
public class SysAdminService {
    @Autowired
    private SysAdminMapper adminMapper;

    private SysAdminExample getExample(SysAdminQo qo){
        SysAdminExample example = new SysAdminExample();
        SysAdminExample.Criteria criteria = example.createCriteria();

        if (qo != null){
            // if (Utils.isNotEmpty(qo.getOrderbyclause())) example.setOrderByClause(qo.getOrderbyclause());
            if (Utils.isNotEmpty(qo.getTel())) criteria.andTelEqualTo( qo.getTel());
            if (Utils.isNotEmpty(qo.getTelLike())) criteria.andTelLike("%"+ qo.getTelLike()+"%");
            if (Utils.isNotEmpty(qo.getCompanyId())) criteria.andCompanyIdEqualTo(qo.getCompanyId());
            if (Utils.isNotEmpty(qo.getName())) criteria.andNameEqualTo(qo.getName());
            if (Utils.isNotEmpty(qo.getNameLike())) criteria.andNameLike("%"+qo.getNameLike()+"%");
            if (Utils.isNotEmpty(qo.getUsername())) criteria.andUsernameEqualTo(qo.getUsername());
            if (Utils.isNotEmpty(qo.getUsernameLike())) criteria.andUsernameLike("%"+qo.getUsernameLike()+"%");
            if (Utils.isNotEmpty(qo.getAdminIdNotEqual())) criteria.andAdminIdNotEqualTo(qo.getAdminIdNotEqual());
            if (Utils.isNotEmpty(qo.getAdminState())) criteria.andAdminStateEqualTo(qo.getAdminState());
            if (Utils.isNotEmpty(qo.getAdminType())) criteria.andAdminTypeEqualTo(qo.getAdminType());
            if (Utils.isNotEmpty(qo.getSysFlag())) criteria.andSysFlagEqualTo(qo.getSysFlag());
            if (qo.getAdminIdList() != null && qo.getAdminIdList().size() > 0) criteria.andAdminIdIn(qo.getAdminIdList());
        }
        return example;
    }

    public Integer count(SysAdminQo qo){
        return adminMapper.countByExample(getExample(qo));
    }

    public List<SysAdmin> findAll() {
        return findAll(null);
    }

    public List<SysAdmin> findAll(SysAdminQo qo) {
        return adminMapper.selectByExample(getExample(qo));
    }

    public List<SysAdmin> findAll(SysAdminQo qo, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return adminMapper.selectByExample(getExample(qo));
    }

    public PageInfo<SysAdmin> pageList(PagingRequest pagingRequest, SysAdminQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum(), pagingRequest.getPageSize());
        return new PageInfo<SysAdmin>(adminMapper.selectByExample(getExample(qo)));
    }

    public SysAdmin findOne(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    public Integer save(SysAdmin record){
        int result = adminMapper.insertSelective(record);
      /*  if(record.getCompanyId() != null && record.getCompanyId() > 0){
            updateCompanyCount(record.getCompanyId(),1);
        }*/
        return result;
    }

    public Integer update(SysAdmin record) {
        SysAdmin oldSysAdmin = null;
        if(record.getCompanyId()!= null ){
            oldSysAdmin = findOne(record.getAdminId());
        }
        Integer result = adminMapper.updateByPrimaryKeySelective(record);
        if(oldSysAdmin != null){
            if(record.getCompanyId() != oldSysAdmin.getCompanyId() ){
                if(record.getCompanyId() != null &&  record.getCompanyId() > 0 ){
                    updateCompanyCount(record.getCompanyId(),1);
                }
                if(oldSysAdmin.getCompanyId() != null &&  oldSysAdmin.getCompanyId() > 0 ){
                    updateCompanyCount(oldSysAdmin.getCompanyId(),-1);
                }
            }
        }
        return result;
    }

    public Integer delete(Integer id){
        SysAdmin admin = findOne(id);
        int result = adminMapper.deleteByPrimaryKey(id);
        updateCompanyCount(admin.getCompanyId(),-1);
        return result;
    }

    public SysAdmin findByUsername(String username) {
        SysAdminExample example = new SysAdminExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<SysAdmin> list = adminMapper.selectByExample(example);
        return (list != null && !list.isEmpty()) ? list.get(0) : null;
    }

    public int deleteWithCompanyId(Long company_id) {
        SysAdminQo qo =new SysAdminQo();
        qo.setCompanyId(company_id);
        int result = adminMapper.deleteByExample(getExample(qo));
        updateCompanyCount(company_id,-1);
        return result;
    }

    /**
     * 修改子公司数量
     * @param company_id
     * @return
     */
    private void updateCompanyCount(Long company_id,Integer num){
     /*   JSONObject jsonObject = new JSONObject();
        jsonObject.put("company_id",company_id);
        jsonObject.put("count",num);
        MqUtils.sendMessage(MqConfig.P_GROUP_COMPANY_ADMIN, MqConfig.TOPIC_ADMIN,MqConfig.TAG_COMPANY_ADMIN_EDIT, String.valueOf(System.currentTimeMillis()), jsonObject.toJSONString());*/
    }

}
